{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training accuracy is 0.9666666666666667\n",
      "Test accuracy is 0.9666666666666667\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import copy\n",
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.20)\n",
    "\n",
    "gnb = GaussianNB()\n",
    "gnb.fit(X_train, y_train)\n",
    "print(\"Training accuracy is %s\"% gnb.score(X_train,y_train))\n",
    "print(\"Test accuracy is %s\"% gnb.score(X_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train accuracy is 0.9666666666666667\n",
      "Test accuracy is 0.9666666666666667\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "\n",
    "class GaussianNB_:\n",
    "    def __init__(self):\n",
    "        self.prior = None\n",
    "        self.avgs = None\n",
    "        self.vars = None\n",
    "        self.n_class = None\n",
    "        \n",
    "    def _get_prior(self, y):\n",
    "        cnt = Counter(y)\n",
    "        prior = np.array([cnt[i] / len(y) for i in range(len(cnt))])\n",
    "        return prior\n",
    "    \n",
    "    def _get_avgs(self, X, y):\n",
    "        return np.array([X[y == i].mean(axis=0) for i in range(self.n_class)])\n",
    "    \n",
    "    def _get_vars(self, X, y):\n",
    "        return np.array([X[y == i].var(axis=0) for i in range(self.n_class)])\n",
    "    \n",
    "    def _get_likelihood(self, row):\n",
    "        return (1 / np.sqrt(2 * np.pi * self.vars) * np.exp(-(row - self.avgs)**2 / (2 * self.vars))).prod(axis=1)\n",
    "    \n",
    "    def fit(self, X, y):\n",
    "        self.prior = self._get_prior(y)\n",
    "        self.n_class = len(self.prior)\n",
    "        self.avgs = self._get_avgs(X, y)\n",
    "        self.vars = self._get_vars(X, y)\n",
    "        \n",
    "    def predict_prob(self, X):\n",
    "        likelihood = np.apply_along_axis(self._get_likelihood, axis=1, arr=X)\n",
    "        probs = self.prior * likelihood\n",
    "        probs_sum = probs.sum(axis=1)\n",
    "        return probs / probs_sum[:, None]\n",
    "    \n",
    "    def predict(self, X):\n",
    "        return self.predict_prob(X).argmax(axis=1)\n",
    "\n",
    "def get_acc(y, y_hat):\n",
    "    a = 0\n",
    "    for i in range(len(y)):\n",
    "        if y[i]==y_hat[i]:\n",
    "            a += 1\n",
    "    return a/len(y)\n",
    "\n",
    "\n",
    "clf = GaussianNB_()\n",
    "clf.fit(X_train, y_train)\n",
    "\n",
    "y_hat = clf.predict(X_train)\n",
    "acc = get_acc(y_train, y_hat)\n",
    "print(\"Train accuracy is %s\"% acc)\n",
    "\n",
    "y_hat = clf.predict(X_test)\n",
    "acc = get_acc(y_test, y_hat)\n",
    "print(\"Test accuracy is %s\"% acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "original data:  [[5.3 3.7 1.5 0.2]]\n",
      "original label:  0\n",
      "attack data:  [[5.3 3.7 3.5 0.2]]\n",
      "predict label:  [1]\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "\n",
    "# Attack on GaussianNB\n",
    "def GaussianNB_attack(clf, X_predict, y_predict): \n",
    "    m = np.diag([0.5,0.5,0.5,0.5])*4\n",
    "    flag = True\n",
    "    for i in range(1,5):\n",
    "        for ii in list(itertools.combinations([0,1,2,3],i)):\n",
    "            delta = np.zeros(4)\n",
    "            for jj in ii:\n",
    "                delta += m[jj]\n",
    "            \n",
    "            y_pre = clf.predict(copy.deepcopy(X_predict)+delta)      \n",
    "            if y_predict != y_pre:\n",
    "                X_predict += delta\n",
    "                flag = False\n",
    "                break\n",
    "                \n",
    "            y_pre = clf.predict(copy.deepcopy(X_predict)-delta)      \n",
    "            if y_predict != y_pre:\n",
    "                X_predict -= delta\n",
    "                flag = False\n",
    "                break\n",
    "        if not flag:\n",
    "            break\n",
    "    \n",
    "    print('attack data: ', X_predict)\n",
    "    print('predict label: ', clf.predict(copy.deepcopy(X_predict)))\n",
    "\n",
    "X_test_ = X_test[0:1]\n",
    "y_test_ = y_test[0]\n",
    "print('original data: ', X_test_)\n",
    "print('original label: ', y_test_)\n",
    "GaussianNB_attack(clf, X_test_, y_test_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
